package com.anolesoft.yr.mbus.prod.base.crtl;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.prod.base.dto.*;
import com.anolesoft.yr.mbus.prod.base.entity.ProdBaseService;
import com.anolesoft.yr.mbus.prod.base.entity.ProdBaseServiceValue;
import com.anolesoft.yr.mbus.prod.base.service.ProdBaseServiceService;
import com.anolesoft.yr.mbus.prod.base.vo.ProdBaseServiceQueryVO;
import com.anolesoft.yr.mbus.prod.base.vo.ProdBaseServiceValueQueryVO;
import com.anolesoft.yr.mbus.util.GlobalLoginInfo;
import com.anolesoft.yr.mbus.util.StrCP;
import com.anolesoft.yr.util.PageInfo;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@CrossOrigin
@RequestMapping(value = "/prodBaseService")
public class ProdBaseServiceController {

    @Autowired
    ProdBaseServiceService prodBaseServiceService;

    //新增服务项基础信息表
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult saveProdBaseService(@RequestBody ProdBaseService prodBaseService) throws Exception {
        String prodBaseServiceUid = StrCP.createObjuid32(UUID.randomUUID().toString());
        ProdBaseServiceSaveDTO prodBaseServiceSaveDTO = BeanUtil.copyProperties(prodBaseService, ProdBaseServiceSaveDTO.class);
        prodBaseServiceSaveDTO.setObjuid(prodBaseServiceUid);
        prodBaseServiceSaveDTO.setFacCode("");
        prodBaseServiceSaveDTO.setCcUserUid(GlobalLoginInfo.getGlobalLoginUser());
        prodBaseServiceSaveDTO.setCcTime(GlobalLoginInfo.getGlobalLoginTime());
        prodBaseServiceSaveDTO.setDelFlag(0);

        List prodBaseServiceValueSaveDTOList = Lists.newArrayList();
        List prodBaseServiceValueList = prodBaseService.getProdBaseServiceValue();
        for (int i = 0; i < prodBaseServiceValueList.size(); i++) {
            ProdBaseServiceValue prodBaseServiceValue = (ProdBaseServiceValue) prodBaseServiceValueList.get(i);

            String prodBaseServiceValueUid = StrCP.createObjuid32(UUID.randomUUID().toString());
            ProdBaseServiceValueSaveDTO prodBaseServiceValueSaveDTO = BeanUtil.copyProperties(prodBaseServiceValue, ProdBaseServiceValueSaveDTO.class);
            prodBaseServiceValueSaveDTO.setObjuid(prodBaseServiceValueUid);
            prodBaseServiceValueSaveDTO.setFacCode("");
            prodBaseServiceValueSaveDTO.setProdBaseServiceUid(prodBaseServiceUid);
            prodBaseServiceValueSaveDTO.setCcUserUid(GlobalLoginInfo.getGlobalLoginUser());
            prodBaseServiceValueSaveDTO.setCcTime(GlobalLoginInfo.getGlobalLoginTime());
            prodBaseServiceValueSaveDTO.setDelFlag(0);
            prodBaseServiceValueSaveDTOList.add(prodBaseServiceValueSaveDTO);
        }
        prodBaseServiceSaveDTO.setProdBaseServiceValueSaveDTO(prodBaseServiceValueSaveDTOList);

        String str = "";
        try {
            str = prodBaseServiceService.saveProdBaseService(prodBaseServiceSaveDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }
        return new AjaxResult(200, str);
    }

    //修改服务项基础信息表
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updateProdBaseService(@RequestBody ProdBaseService prodBaseService) throws Exception {
        ProdBaseServiceUpdateDTO prodBaseServiceUpdateDTO = BeanUtil.copyProperties(prodBaseService, ProdBaseServiceUpdateDTO.class);
        prodBaseServiceUpdateDTO.setMmUserUid(GlobalLoginInfo.getGlobalLoginUser());
        prodBaseServiceUpdateDTO.setMmTime(GlobalLoginInfo.getGlobalLoginTime());

        Map<String, Object> maps = new <String, Object>HashMap();
        List prodBaseServiceValueSaveDTOList = Lists.newArrayList();
        List prodBaseServiceValueUpdateDTOList = Lists.newArrayList();
        List prodBaseServiceValueDeleteDTOList = Lists.newArrayList();
        ProdBaseServiceValueDeleteDTO prodBaseServiceValueDeleteDTO = null;
        List prodBaseServiceValueList = prodBaseService.getProdBaseServiceValue();
        for (int i = 0; i < prodBaseServiceValueList.size(); i++) {
            ProdBaseServiceValue prodBaseServiceValue = (ProdBaseServiceValue) prodBaseServiceValueList.get(i);

            if ("".equals(prodBaseServiceValue.getObjuid())) {
                String prodBaseServiceValueUid = StrCP.createObjuid32(UUID.randomUUID().toString());

                ProdBaseServiceValueSaveDTO prodBaseServiceValueSaveDTO = BeanUtil.copyProperties(prodBaseServiceValue, ProdBaseServiceValueSaveDTO.class);
                prodBaseServiceValueSaveDTO.setObjuid(prodBaseServiceValueUid);
                prodBaseServiceValueSaveDTO.setFacCode("");
                prodBaseServiceValueSaveDTO.setProdBaseServiceUid(prodBaseService.getObjuid());
                prodBaseServiceValueSaveDTO.setCcUserUid(GlobalLoginInfo.getGlobalLoginUser());
                prodBaseServiceValueSaveDTO.setCcTime(GlobalLoginInfo.getGlobalLoginTime());
                prodBaseServiceValueSaveDTO.setDelFlag(0);
                prodBaseServiceValueSaveDTOList.add(prodBaseServiceValueSaveDTO);

                prodBaseServiceValueDeleteDTO = new ProdBaseServiceValueDeleteDTO();
                prodBaseServiceValueDeleteDTO.setObjuid(prodBaseServiceValueUid);
                prodBaseServiceValueDeleteDTOList.add(prodBaseServiceValueDeleteDTO);
            } else {
                ProdBaseServiceValueUpdateDTO prodBaseServiceValueUpdateDTO = BeanUtil.copyProperties(prodBaseServiceValue, ProdBaseServiceValueUpdateDTO.class);
                prodBaseServiceValueUpdateDTO.setMmUserUid(GlobalLoginInfo.getGlobalLoginUser());
                prodBaseServiceValueUpdateDTO.setMmTime(GlobalLoginInfo.getGlobalLoginTime());
                prodBaseServiceValueUpdateDTOList.add(prodBaseServiceValueUpdateDTO);

                prodBaseServiceValueDeleteDTO = new ProdBaseServiceValueDeleteDTO();
                prodBaseServiceValueDeleteDTO.setObjuid(prodBaseServiceValue.getObjuid());
                prodBaseServiceValueDeleteDTOList.add(prodBaseServiceValueDeleteDTO);
            }
        }
        prodBaseServiceUpdateDTO.setProdBaseServiceValueUpdateDTO(prodBaseServiceValueUpdateDTOList);

        Map deleteMap = new HashMap();
        deleteMap.put("prodBaseServiceDeleteUid", prodBaseService.getObjuid());
        deleteMap.put("prodBaseServiceValueDeleteDTOList", prodBaseServiceValueDeleteDTOList);

        maps.put("save", prodBaseServiceValueSaveDTOList);
        maps.put("update", prodBaseServiceUpdateDTO);
        maps.put("delete", deleteMap);

        String str = "";
        try {
            str = prodBaseServiceService.updateProdBaseService(maps);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }

        return new AjaxResult(200, str);
    }

    //删除服务项基础信息表
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult deleteProdBaseService(@RequestBody List<ProdBaseService> prodBaseServiceList) throws Exception {
        List prodBaseServiceDeleteDTOList = new ArrayList();
        ProdBaseServiceDeleteDTO prodBaseServiceDeleteDTO = null;

        for (int i = 0; i < prodBaseServiceList.size(); i++) {
            ProdBaseService prodBaseService = prodBaseServiceList.get(i);

            prodBaseServiceDeleteDTO = new ProdBaseServiceDeleteDTO();
            prodBaseServiceDeleteDTO.setObjuid(prodBaseService.getObjuid());
            prodBaseServiceDeleteDTOList.add(prodBaseServiceDeleteDTO);
        }

        String str = "";
        try {
            str = prodBaseServiceService.deleteProdBaseService(prodBaseServiceDeleteDTOList);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }
        return new AjaxResult(200, str);
    }

    //根据主键查询服务项基础唯一数据
    @RequestMapping(value = "/queryByUid", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult AjaxResult(@RequestBody ProdBaseServiceQueryDTO prodBaseServiceQueryDTO) throws Exception {
        Map<String, Object> maps = prodBaseServiceService.queryProdBaseServiceByUid(prodBaseServiceQueryDTO.getObjuid());
        ProdBaseServiceQueryVO prodBaseServiceQueryVO = (ProdBaseServiceQueryVO) maps.get("prodBaseServiceQueryVO");
        List prodBaseServiceValueQueryVOList = (List) maps.get("prodBaseServiceValueQueryVOList");

        String jsons = "";
        jsons += "{";
        jsons += "\"prodBaseService\":";
        if (prodBaseServiceQueryVO != null) {
            jsons += "[";
            jsons += new Gson().toJson(prodBaseServiceQueryVO);
            jsons += "]";
        }
        jsons += ",\"prodBaseServiceValue\":";
        jsons += "[";
        if (prodBaseServiceValueQueryVOList.size() > 0) {
            for (int i = 0; i < prodBaseServiceValueQueryVOList.size(); i++) {
                ProdBaseServiceValueQueryVO prodBaseServiceValueQueryVO = (ProdBaseServiceValueQueryVO) prodBaseServiceValueQueryVOList.get(i);
                jsons += new Gson().toJson(prodBaseServiceValueQueryVO);
                if (i < (prodBaseServiceValueQueryVOList.size() - 1)) {
                    jsons += ",";
                }
            }
        }
        jsons += "]";
        jsons += "}";

        return new AjaxResult(200, "SUCCESS", jsons);
    }

    //根据条件查询服务项基础所有数据
    @RequestMapping(value = "/queryAll", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryProdBaseServiceAll(String baseServiceName, int page, int rows) throws Exception {//@RequestBody ProdBaseServiceQueryDTO prodBaseServiceQueryDTO
        Map<String, Object> maps = prodBaseServiceService.queryProdBaseServiceAll(baseServiceName, page, rows);
        int count = (int) maps.get("count");
        List prodBaseServiceQueryVOList = (List) maps.get("prodBaseServiceQueryVOList");

        return new AjaxResult(200, "SUCCESS", new PageInfo(count, prodBaseServiceQueryVOList));
    }

}
